package com.njitzx.controller;


import com.njitzx.entity.po.Role;
import com.njitzx.entity.vo.PermVO;
import com.njitzx.entity.vo.RefreshPerVO;
import com.njitzx.entity.vo.RoleVO;
import com.njitzx.exception.RoleException;
import com.njitzx.service.RoleService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import com.njitzx.entity.vo.Result;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

import lombok.RequiredArgsConstructor;


import org.springframework.web.bind.annotation.RestController;


/**
 * <p>
 * 角色表 前端控制器
 * </p>
 *
 * @author 周鑫
 * @since 2024-08-20
 */
@Api(tags = "角色表接口")
@RestController
@RequestMapping("/roles")
@RequiredArgsConstructor
public class RoleController {

    private final RoleService roleService;

//    @ApiOperation("角色表分页查询")
//    @GetMapping("/list")
//    public Result findPage(@ApiParam("页码") @RequestParam(defaultValue = "1") Integer pageNum,
//                               @ApiParam("页大小") @RequestParam(defaultValue = "5") Integer pageSize,
//                               @ApiParam("查询条件") Role role){
//        PageInfo<Role> pageInfo = roleService.findPage(pageNum,pageSize,role);
//        return Result.success(pageInfo);
//    }

    @ApiOperation("查询所有角色列表")
    @GetMapping
    public Result findAll() {
        List<Role> list = roleService.findAll();
        return Result.success(list);
    }

    @ApiOperation("根据主键查询角色表")
    @GetMapping("/{roleId}")
    public Result findById(@PathVariable Serializable roleId) {
        return Result.success(roleService.findById(roleId));
    }

    @ApiOperation("新增角色表")
    @PostMapping
    public Result add(@RequestBody Map<String,String> map){
        String roleName = map.get("roleName");
        String roleDesc = map.get("roleDesc");
        roleService.addRole(roleName, roleDesc);
        return Result.success();
    }

    @ApiOperation("修改角色表")
    @PutMapping("/{roleId}")
    public Result modify(@PathVariable Integer roleId, @RequestBody Role role) {
        if (role.getRoleName() == null) {
            throw new RoleException("角色名称不能为空");
        }
        role.setRoleId(roleId);
        roleService.modify(role);
        return Result.success();
    }

    @ApiOperation("根据主键删除角色表")
    @DeleteMapping("/{roleId}")
    public Result removeById(@PathVariable Serializable roleId) {
        roleService.removeById(roleId);
        return Result.success();
    }

    @ApiOperation("角色授权")
    @PostMapping("/{roleId}/rights")
    public Result grandRoleByRoleId(@PathVariable(value = "roleId", required = true) Integer roleId, @RequestBody Map<String, Object> map) {
        String ids = (String) map.get("rids");
        roleService.grandRole(roleId, ids);
        return Result.success();
    }

//    @ApiOperation("删除角色指定权限")
//    @DeleteMapping("/{roleId}/rights/{rightId}")
//    public Result removeRoleRight(@PathVariable(value = "roleId", required = true) Integer roleId, @RequestBody Map<String, Object> map) {
//        String ids = (String) map.get("rids");
////        roleService.removeRoleRight(roleId,ids);
//        return Result.success();
//    }

    @ApiOperation("角色权限列表")
    @GetMapping("/right")
    public Result findRoleRightList() {
        List<RoleVO> roleVOList = roleService.findrRoeList();
        return Result.success(roleVOList);
    }


    @ApiOperation("删除角色指定权限")
    @DeleteMapping("/{roleId}/rights/{rightId}")
    public Result deleteRolePermission(@PathVariable Integer roleId, @PathVariable Integer rightId) {
        List<PermVO> permVOS = roleService.deleteRolePermission(roleId, rightId);
        return Result.success(permVOS);
    }
}
